package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ActivityPlanService;
import com.qdlc.p2p.biz.service.ExperienceRuleService;
import com.qdlc.p2p.biz.service.RedEnvelopeService;
import com.qdlc.p2p.biz.service.UpRateService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.ActivityPlan;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.dto.RateRule;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.model.ActivityPlanModel;

/**
 * 活动方案 后台管理
 * @author zlhx
 * @version 2.0
 * @date 2015-04-25 12:10
 */
public class ManageActivityPlanAction extends BaseAction<ActivityPlanModel> implements ModelDriven<ActivityPlanModel>{
	@Resource
	private ActivityPlanService activityPlanService;
	
	
	@Resource
	private ExperienceRuleService experienceRuleService;
	
	@Resource
	private UpRateService upRateService;
	
	@Resource
	private RedEnvelopeService redEnvelopeService;
	
	private Map<String, Object> data;
	
	public Map<String, Object> getData() {
		return data;
	}
	public void setData(Map<String, Object> data) {
		this.data = data;
	}
	
	/**
	 * 活动方案管理
	 * @return
	 */
	@Action("/modules/additional/activityPlan/activityPlanManager")
	public String activityPlanManager(){
		return "activityPlanManager";
	}
	
	/**
	 * 活动方案列表查询
	 * @throws IOException
	 */
	@Action(value = "/modules/additional/activityPlan/doActivityPlan")
	public void activityPlanList() throws IOException{
		data = new HashMap<String, Object>();
		activityPlanService = (ActivityPlanService) BeanUtil.getBean("activityPlanService");
		PageDataList<ActivityPlanModel> plist = activityPlanService.manageList(model);
		data.put("total", plist.getPage().getTotal());
		data.put("rows", plist.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 修改活动方案
	 * @return
	 */
	@Action(value = "/modules/additional/activityPlan/activityPlanModify")
	public String modify(){
		ActivityPlan activityPlan = activityPlanService.findById(model.getId());
		//开启的体验券规则列表
		List<ExperienceRule> experienceRuleList = experienceRuleService.findUsableExperienceRule();
		//开启的加息券规则列表
		List<RateRule> rateRuleList = upRateService.getUseRateRuleList();
		//开启的红包规则列表
		List<RedEnvelope> redEnvelopeList = redEnvelopeService.findUsableRedEnvelope();
		request.setAttribute("activityPlan", activityPlan);
		request.setAttribute("experienceRuleList", experienceRuleList);
		request.setAttribute("rateRuleList", rateRuleList);
		request.setAttribute("redEnvelopeList", redEnvelopeList);
		return "activityPlanModify";
	}
	
	/**
	 * 执行修改活动方案
	 * @return
	 * @throws IOException 
	 */
	@Action(value = "/modules/additional/activityPlan/doModify")
	public void doModify() throws IOException{
		model.validModelData();
		//ActivityPlan activityPlan = model.prototypeByModify();
		activityPlanService.update(model);
		data = new HashMap<String, Object>();
		data.put("result", "true");
		data.put("msg", "修改成功");
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 启用活动方案
	 * @throws IOException
	 */
	@Action(value = "/modules/additional/activityPlan/activityPlanOpen")
	public void activityPlanOpen() throws IOException{
		int result = activityPlanService.update(ActivityPlanModel.ACTIVITYPLAN_STATUS_OPEN,ActivityPlanModel.ACTIVITYPLAN_STATUS_CLOSE, model.getId());
		if(result>0){
			printWebSuccess();
		}else{
			printWebResult("启用失败，请确认活动方案是否已开启！", false);
		}
	}
	
	/**
	 * 关闭活动方案
	 * @throws IOException
	 */
	@Action(value = "/modules/additional/activityPlan/activityPlanClose")
	public void activityPlanClose() throws IOException{
		int result = activityPlanService.update(ActivityPlanModel.ACTIVITYPLAN_STATUS_CLOSE,ActivityPlanModel.ACTIVITYPLAN_STATUS_OPEN, model.getId());
		if(result>0){
			printWebSuccess();
		}else{
			printWebResult("关闭失败，请确认活动方案是否已关闭！", false);
		}
	}
	
	
}
